<?php
    /**
    * @desc:Magic library which having all basic needed functions used to run framwork. 
    */
    include_once 'library/magic.lib.php';
    /**
    * @desc : This is class is used to load other classes into one class .
    * So ,Logically it provide muliple inheritance functionality to the base class . 
    */
    Magic::callMethod()->import(LIB_PATH . 'inc'.DS.'Magic_ClassUpload_Model', 'inc');

    /**
    * @desc: This is the base controller . Any controller in module must inherit this controller.
    * If not than functionality will not work properly . 
    */
    Magic::callMethod()->import(LIB_PATH . 'base_controller');

  //  Magic::callMethod()->import(LIB_PATH . 'inc/Magic_Varmaker_Lib', 'inc');
    /**
    * @desc : This is the template class . Use of this class to render template into application. 
    */
    Magic::callMethod()->import(LIB_PATH . 'template.lib');
    /**
    * 
    */
    Magic::callMethod()->import(LIB_PATH . 'session.lib');
    /**
    * @desc : This is bootstrap class of application .
    * Use to handel all functionality of framework .
    * To work framework properly please do not modified this file . 
    */
    Magic::callMethod()->importLibarary('Magic_RedIniFile_Model');
    Magic::callMethod()->importLibarary('Magic_Connection_Model');
    Magic::callMethod()->importLibarary('Magic_Common_Model');
    Magic::callMethod()->importLibarary('Magic_Cache');
    Magic::callMethod()->import(LIB_PATH . 'acl.lib', NULL, false);
    global $cssArray ,$jsArray ;
    /**
    * @desc : Role object  
    */
    class bootstrap {

        /**
        * @desc : Protected variable to define name of model .
        */
        protected $model;

        /**
        * @desc: Protected variable use to difine controller.
        */
        protected $controller;

        /**
        * @desc: Public variable use to difine view.
        */
        public $view;

        /**
        * @desc: Public variable use to difine controller class of particular module.
        */
        public $controllerClass;

        /**
        * @desc: Public variable use to define action event of request.
        */
        public $method;
        /**
         *@desc: NAme of application folder model name 
         * @var type 
         */
        public $appcall ; 
        
        /**
        * @desc: contructor of bootstrap define all variable on creation of object .
        */
        public function __construct() {
            if(ACL_ENABLE == true){
                Magic::callMethod()->import(ROOT.DS."log".DS."application.acl");
                global $role ; 
                global $aclObj;
                $aclObj = MAGIC_ACL::callMethod()->bindArrayToObject($role);
            }
            $this->model = INCLUDE_PATH . Magic::callMethod()->getAppDirectory() . DS . M_D . DS . Magic::callMethod()->getModel();
            $this->controller = INCLUDE_PATH . Magic::callMethod()->getAppDirectory() . DS . C_D . DS . Magic::callMethod()->getController(); 
            $this->view = INCLUDE_PATH . Magic::callMethod()->getAppDirectory() . DS . V_D . DS .Magic::callMethod()->getController(true).DS.strtolower(Magic::callMethod()->getView()); 
            $this->controllerClass = Magic::callMethod()->getController();
            $this->method = Magic::callMethod()->getViewAction();
            $this->appcall = str_replace('_'.M_D, '', Magic::callMethod()->getModel())."|".Magic::callMethod()->getController(true)."|".str_replace('_'.V_D,'',Magic::callMethod()->getView());
        }
        /**
        * @desc: This class is used to include controller and module file which indicate by request url
        * @return \ErrorException 
        */
        public function autoLoader() {
            if ($this->checkMVC($this->model . Magic::$ext)) {
                Magic::callMethod()->import($this->model, '', false);
                if ($this->checkMVC($this->controller . Magic::$ext)) {
                    Magic::callMethod()->import($this->controller, '', false);
                } else {
                    return new ErrorException();
                }
            } elseif (!$this->checkMVC($this->model . Magic::$ext)) {
                if ($this->checkMVC($this->controller . Magic::$ext)) {
                    Magic::callMethod()->import($this->controller, '', false);
                } else {
                    return new ErrorException();
                }
            }
        }
        /**
        * @desc: this function is used to check avability of particular file 
        * @param $filename: name of file 
        * @return boolean 
        */
        public function checkMVC($filename) {
            if (file_exists($filename)) {
                return true;
            } else {
                return false;
            }
        }

        /**
        * @desc : Used to redirect url on with index file  
        */
        public function bootStrapRedirect($enable = false, $sessionPara = NULL, $loginmodule = array()) {
            if ($_SERVER["REQUEST_URI"] == '/') {
                Magic_Common_Model::callMethod()->_pageRedirect("index.php");
            }
        }
        /**
         * @desc : This function is use to define view  
         */
        public function defineView (){
            echo $this->view.Magic::$ext ;
        }
        /**
         *
         * @param type $iniKey
         * @param type $iniVal
         * @return type 
         */
        public function readINI($iniKey, $iniVal) {
            return Magic_ReadIniFile_Model::callMethod()->readIniValue(CONFIG_PATH, $iniKey, $iniVal);
        }
        
        public function isAllow() {
            //global $aclObj;
            //echo $this->appcall; 
            MAGIC_ACL::callMethod()->isAllow($this->appcall);
            return true  ; 
        }
    }
    /**
    * @desc : Object of bootstrap class . 
    */
    $bootstrap = new bootstrap();
    $bootstrap->autoLoader();
   
    if (Magic::callMethod()->getBaseClass($bootstrap->controllerClass)) {
        /**
        * @desc : Check controller Method exist 
        * If exist than do appropriate operation  
        */
        if (method_exists($bootstrap->controllerClass, $bootstrap->method)) {
            /**
            * @desc : initialy call contructor function to create object of the model 
            */
            $objC = new $bootstrap->controllerClass();
            Magic::callMethod()->copyObj($objC);
            $ref = new ReflectionClass($objC);
            call_user_func_array(array($ref, 'newInstanceArgs'), array());
            /**
            * @desc: Here Action Method Call from controller file  
            */
            call_user_func_array(array($objC, $bootstrap->method), array(''));
            /**
            * @desc :Check if ACL Enable of not 
            */
            if(ACL_ENABLE == true) {   
                
                /**
                * @desc : If ACL_ENABLE == true 
                * Check for user authentication 
                */
                if($bootstrap->isAllow()) {
                    try {
                       /* ob_start();
                        $content = ob_get_contents();
                        ob_flush();
                        ob_clean();
                         */
                        
                        include $bootstrap->view.Magic::$ext ;
                        //Magic::callMethod()->fileWrite($bootstrap->appcall,$content);
                
                    } catch (Exception $e) {
                        echo $e->getMessage();
                    }
                } else {
                    echo "<h1>404 Not found .</h1>";
                } 
            } else {
                try {
                    include $bootstrap->view.Magic::$ext ; 
                } catch (Exception $e) {
                    echo $e->getMessage();
                }
            }
        } else {
            echo new ErrorException();
        }
    } else {
        echo new ErrorException("Please inherit base_controller in your controller class");
    }
?>
